
package com.b2c.service;

import com.b2c.mapper.CategoryMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.b2c.common.utils.PageResult;
import com.mr.service.pojo.BrandEntity;
import com.b2c.mapper.BrandMapper;
import com.mr.service.pojo.CategoryEntity;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BrandService
 * @Description: TODO
 * @Author shenyaqi
 * @Date 2020/7/18
 * @Version V1.0
 **/
@Service
public class BrandService {
    @Autowired
    private BrandMapper mapper;


    public PageResult<BrandEntity> getBrandPage(Integer page, Integer rows,String key, String sortBy, Boolean desc){

        PageHelper.startPage(page,rows);

        /*过滤条件*/
        Example example = new Example(BrandEntity.class);
        if (key!=null && key!=""){
            //设置模糊查询
            example.createCriteria().andLike("name","%"+key.trim()+"%");
        }

        if (sortBy!=null && sortBy.equals("")){
            example.setOrderByClause(sortBy+ (desc?" asc ":" desc "));
        }
        Page<BrandEntity> pageInfo = (Page<BrandEntity>) mapper.selectByExample(example);
        return new PageResult<BrandEntity>(pageInfo.getTotal(),pageInfo.getResult());
    }

    //品牌新增
    @Transactional
    public void addBrand(BrandEntity entity, List<Long> cids){

        mapper.insertSelective(entity);
        //新增中间表品牌下的分类

        //调用for循环
        for(long cid:cids){
            mapper.insertCategoryBrand(cid,entity.getId());
        }
    }

    @Transactional
    public void updateBrand(BrandEntity entity, List<Long> cids){

        mapper.updateByPrimaryKey(entity);

        mapper.delBrandCateByBid(entity.getId());
        //调用for循环
        for(long cid:cids){
            mapper.insertCategoryBrand(cid,entity.getId());
          }
    }

    @Transactional
    public void deleteBrand(Long bid) {
        
        mapper.delBrandCateByBid(bid);

        mapper.deleteByPrimaryKey(bid);

    }

    public List<BrandEntity> queryBrandByCid(Long cid) {
        return this.mapper.queryBrandByCid(cid);
    }

    public BrandEntity queryBrandById(Long id) {

        return  mapper.selectByPrimaryKey(id);
    }
    /**
     * 根据id查询品牌
     * @param id
     * @return
     */



}